<?php

require_once "../common/citizen.php";
require_once "../common/curl.php";

$string_state			= "//div[@class='citizen_state']/div/span";
$string_level			= "//div[@class='citizen_experience']/strong";
$string_strength	= "//div[@class='citizen_military'][1]/h4";
$string_rank			= "//div[@class='citizen_military'][2]/div/small/strong";
$string_avatar		= "//img[@class='citizen_avatar']";	
$string_work			= "//td[@class='stat']/small";


class DataFile {
	var $handle;

	function Open($filename)
	{
		$this->handle = fopen($filename, 'a');
		return $this->handle;
	}

	function Close()
	{
		fclose($this->handle);
	}

	function Write($obj)
	{
		$str = $obj->Output();
		fwrite($this->handle, $str);
	}
	
}

$tmp_data = new DataFile();

function Parse($id, $tag)
{
	global $string_state, $string_level, $string_strength;
	global $string_rank, $string_avatar, $string_work;
	global $tmp_data;
	global $myCURL;

	$filename = "http://www.erepublik.com/en/citizen/profile/$id";
	do {
		$file = $myCURL->get($filename);
	} while ($file === FALSE);
	
	$doc = new DOMDocument();
	$doc->loadHTML($file);
	$xpath = new DOMXpath($doc);
	
	$level = 0;
	$strength = 0;
	$rank = 0;
	$work = 0;
	$avatar = '';
	$name = '';
	$dead = FALSE;
	
	
	// get avatar
	$elements = $xpath->query($string_avatar);
	if ($elements->length > 0) {
		$element = $elements->item(0);
		$tmp =  $element->getAttribute('style');
		$start = strpos($tmp, 'http://');
		$end = strpos($tmp, '.jpg');
		$avatar = substr($tmp, $start, $end-$start+4);
		$avatar = str_replace('142x142', '55x55', $avatar);
		$name = trim($element->getAttribute('alt'));
	} else {
		die("Page Error\r\n");
	}
	
	
	//get state
	$elements = $xpath->query($string_state);
	if ($elements->length > 0) {
		$state = $elements->item(0)->textContent;
		if ( stripos($state, 'permanent') !== FALSE || stripos($state, 'dead') !== FALSE ) {
			$dead = TRUE;
		}
	}

	
	// get level
	$elements = $xpath->query($string_level);
	if ($elements->length > 0) {
		$level = intval($elements->item(0)->textContent);
	} else {
		die("Page Error\r\n");
	}
	
	
	// get strength
	$elements = $xpath->query($string_strength);
	if ($elements->length > 0) {
		$temp = str_replace(',', '', $elements->item(0)->textContent);
		$strength  = intval($temp);
	} else {
		die("Page Error\r\n");
	}
	
	
	// get rank
	$elements = $xpath->query($string_rank);
	if ($elements->length > 0) {
		$temp = str_replace(',', '', $elements->item(0)->textContent);
		$rank = intval($temp);
	} else {
		die("Page Error\r\n");
	}

/*
	// get work
	$elements = $xpath->query($string_work);
	if ($elements->length > 0) {
		$temp = str_replace(',', '', $elements->item(0)->textContent);
		$work = intval($temp);
	} else {
		die("Page Error\r\n");
	}
*/
	
	$obj = new Citizen($id,$name,$tag,$level,$strength,$rank,$work,$avatar,$dead);
	$tmp_data->Write($obj);
}

// main

if ($argc != 2) {
	die("Usage: $argv[0] idx_file\n");
}

$tmp_file = 'tmp.dat';
$data_file = 'data.dat';

$ids = file($argv[1], FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$lines = array();
if (file_exists($tmp_file)) {
	$lines = file($tmp_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
}

$myCURL = new cUrl();
$start_line = count($lines);
$end_line = count($ids);

$tmp_data->Open($tmp_file);

libxml_use_internal_errors(true);

for ($i = $start_line; $i < $end_line; $i++) {
	$a = explode(',', trim($ids[$i]));
	Parse($a[0], (count($a)>2 ? $a[2] : 'notog'));
	$total = $i+1;
	echo "------Parsed $total citizen------\n";
}

$tmp_data->Close();

unlink($data_file);
copy($tmp_file, $data_file);
unlink($tmp_file);

?>